{% extends "base.html" %}

{% block head %}
<style>
/* 液态玻璃页面标题 */
.page-title {
    background: var(--glass-bg);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1px solid var(--glass-border);
    border-radius: var(--border-radius);
    padding: 1rem 1.5rem;
    margin-bottom: 1.5rem;
    box-shadow: var(--glass-shadow);
    position: relative;
    overflow: hidden;
}

.page-title::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
    pointer-events: none;
    z-index: 0;
}

.page-title h2 {
    color: #1C1C1E !important;
    text-shadow: 0 2px 4px rgba(0, 122, 255, 0.1);
    position: relative;
    z-index: 1;
    margin: 0;
    font-size: 1.5rem;
}

/* 液态玻璃卡片 */
.card {
    background: var(--glass-bg) !important;
    backdrop-filter: var(--glass-blur) !important;
    -webkit-backdrop-filter: var(--glass-blur) !important;
    border: 1px solid var(--glass-border) !important;
    border-radius: var(--border-radius) !important;
    box-shadow: var(--glass-shadow) !important;
    transition: var(--transition) !important;
    position: relative;
    overflow: hidden;
    margin-bottom: 1.5rem !important;
}

.card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
    pointer-events: none;
    z-index: 0;
}

.card:hover {
    transform: translateY(-5px);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2) !important;
}

.card-header {
    background: rgba(255, 255, 255, 0.1) !important;
    backdrop-filter: var(--glass-blur-light) !important;
    -webkit-backdrop-filter: var(--glass-blur-light) !important;
    border-bottom: 1px solid var(--glass-border) !important;
    padding: 1rem 1.5rem !important;
    position: relative;
    z-index: 1;
}

.card-header span {
    color: #1C1C1E !important;
    font-weight: 600 !important;
    font-size: 1.1rem !important;
}

.card-body {
    position: relative;
    z-index: 1;
    color: #1C1C1E !important;
    padding: 1.5rem !important;
}

/* 液态玻璃表格 - 使用最强选择器覆盖Bootstrap */
.table.table.table {
    background: rgba(255, 255, 255, 0.05) !important;
    backdrop-filter: var(--glass-blur-light) !important;
    -webkit-backdrop-filter: var(--glass-blur-light) !important;
    border-radius: var(--border-radius) !important;
    overflow: hidden !important;
    position: relative;
}

.table.table.table::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.02) 0%, transparent 100%);
    pointer-events: none;
    z-index: 0;
}

.table.table.table thead {
    background: rgba(255, 255, 255, 0.1) !important;
    backdrop-filter: var(--glass-blur-light) !important;
    -webkit-backdrop-filter: var(--glass-blur-light) !important;
    position: relative;
    z-index: 1;
}

.table.table.table thead th {
    background: transparent !important;
    border-bottom: 1px solid var(--glass-border) !important;
    color: #1C1C1E !important;
    font-weight: 600 !important;
    padding: 1rem !important;
    position: relative;
    z-index: 1;
}

.table.table.table tbody {
    position: relative;
    z-index: 1;
}

.table.table.table tbody tr {
    background: rgba(255, 255, 255, 0.05) !important;
    transition: var(--transition) !important;
}

.table.table.table tbody tr:hover {
    background: rgba(255, 255, 255, 0.1) !important;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.table.table.table tbody td {
    border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
    color: #1C1C1E !important;
    padding: 1rem !important;
    vertical-align: middle !important;
}

/* 液态玻璃按钮 */
.btn {
    background: var(--glass-bg) !important;
    backdrop-filter: var(--glass-blur-light) !important;
    -webkit-backdrop-filter: var(--glass-blur-light) !important;
    border: 1px solid var(--glass-border) !important;
    transition: var(--transition) !important;
    border-radius: 8px !important;
    font-weight: 600 !important;
    position: relative;
    overflow: hidden;
}

.btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
    pointer-events: none;
    z-index: 0;
}

.btn > * {
    position: relative;
    z-index: 1;
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15) !important;
}

.btn-primary {
    color: #1C1C1E !important;
    padding: 0.5rem 1rem !important;
}

.btn-primary:hover {
    background: rgba(255, 255, 255, 0.15) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #1C1C1E !important;
}

.btn-warning {
    color: #1C1C1E !important;
    padding: 0.5rem 1rem !important;
}

.btn-warning:hover {
    background: rgba(255, 255, 255, 0.15) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #1C1C1E !important;
}

.btn-danger {
    color: #1C1C1E !important;
    padding: 0.5rem 1rem !important;
}

.btn-danger:hover {
    background: rgba(255, 255, 255, 0.15) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #1C1C1E !important;
}

.btn-success {
    color: #1C1C1E !important;
    padding: 0.5rem 1rem !important;
}

.btn-success:hover {
    background: rgba(255, 255, 255, 0.15) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #1C1C1E !important;
}

.btn-info {
    color: #1C1C1E !important;
    padding: 0.5rem 1rem !important;
}

.btn-info:hover {
    background: rgba(255, 255, 255, 0.15) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #1C1C1E !important;
}

.btn-sm {
    padding: 0.375rem 0.75rem !important;
    font-size: 0.875rem !important;
}

/* 液态玻璃徽章 */
.badge {
    backdrop-filter: var(--glass-blur-light) !important;
    -webkit-backdrop-filter: var(--glass-blur-light) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    font-weight: 600 !important;
    transition: var(--transition) !important;
    padding: 0.5rem 0.75rem !important;
    border-radius: 6px !important;
}

.badge:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    transform: translateY(-1px);
}

/* 暗色模式适配 - 使用最强的选择器覆盖Bootstrap */
body[data-theme="dark"] .page-title,
html[data-theme="dark"] .page-title {
    background: rgba(0, 0, 0, 0.3) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
}

body[data-theme="dark"] .page-title::before,
html[data-theme="dark"] .page-title::before {
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.02) 0%, transparent 100%) !important;
}

body[data-theme="dark"] .page-title h2,
html[data-theme="dark"] .page-title h2 {
    color: #ffffff !important;
}

body[data-theme="dark"] .card,
html[data-theme="dark"] .card {
    background: rgba(0, 0, 0, 0.3) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
}

body[data-theme="dark"] .card::before,
html[data-theme="dark"] .card::before {
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.02) 0%, transparent 100%) !important;
}

body[data-theme="dark"] .card-header,
html[data-theme="dark"] .card-header {
    background: rgba(0, 0, 0, 0.2) !important;
    color: #ffffff !important;
}

body[data-theme="dark"] .card-header span,
html[data-theme="dark"] .card-header span {
    color: #ffffff !important;
}

body[data-theme="dark"] .card-body,
html[data-theme="dark"] .card-body {
    color: #ffffff !important;
}

/* 暗色模式表格 - 使用最强选择器覆盖Bootstrap */
body[data-theme="dark"] .table.table.table,
html[data-theme="dark"] .table.table.table {
    background: rgba(0, 0, 0, 0.2) !important;
}

body[data-theme="dark"] .table.table.table::before,
html[data-theme="dark"] .table.table.table::before {
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.02) 0%, transparent 100%) !important;
}

body[data-theme="dark"] .table.table.table thead,
html[data-theme="dark"] .table.table.table thead {
    background: rgba(0, 0, 0, 0.3) !important;
}

body[data-theme="dark"] .table.table.table thead th,
html[data-theme="dark"] .table.table.table thead th {
    color: #ffffff !important;
    border-bottom-color: rgba(255, 255, 255, 0.1) !important;
}

body[data-theme="dark"] .table.table.table tbody tr,
html[data-theme="dark"] .table.table.table tbody tr {
    background: rgba(0, 0, 0, 0.2) !important;
}

body[data-theme="dark"] .table.table.table tbody tr:hover,
html[data-theme="dark"] .table.table.table tbody tr:hover {
    background: rgba(0, 0, 0, 0.3) !important;
}

body[data-theme="dark"] .table.table.table tbody td,
html[data-theme="dark"] .table.table.table tbody td {
    color: #ffffff !important;
    border-bottom-color: rgba(255, 255, 255, 0.1) !important;
}

body[data-theme="dark"] .btn-primary,
html[data-theme="dark"] .btn-primary {
    background: rgba(0, 0, 0, 0.3) !important;
    color: #ffffff !important;
}

body[data-theme="dark"] .btn-primary:hover,
html[data-theme="dark"] .btn-primary:hover {
    background: rgba(0, 0, 0, 0.4) !important;
    color: #ffffff !important;
}

body[data-theme="dark"] .btn-warning,
html[data-theme="dark"] .btn-warning {
    background: rgba(0, 0, 0, 0.3) !important;
    color: #ffffff !important;
}

body[data-theme="dark"] .btn-warning:hover,
html[data-theme="dark"] .btn-warning:hover {
    background: rgba(0, 0, 0, 0.4) !important;
    color: #ffffff !important;
}

body[data-theme="dark"] .btn-danger,
html[data-theme="dark"] .btn-danger {
    background: rgba(0, 0, 0, 0.3) !important;
    color: #ffffff !important;
}

body[data-theme="dark"] .btn-danger:hover,
html[data-theme="dark"] .btn-danger:hover {
    background: rgba(0, 0, 0, 0.4) !important;
    color: #ffffff !important;
}

body[data-theme="dark"] .btn-info,
html[data-theme="dark"] .btn-info {
    background: rgba(0, 0, 0, 0.3) !important;
    color: #ffffff !important;
}

body[data-theme="dark"] .btn-info:hover,
html[data-theme="dark"] .btn-info:hover {
    background: rgba(0, 0, 0, 0.4) !important;
    color: #ffffff !important;
}

/* 确保暗色模式下所有文字都清晰可见 - 使用最强的选择器 */
body[data-theme="dark"] .table.table.table tbody td i,
html[data-theme="dark"] .table.table.table tbody td i {
    color: #ffffff !important;
}

body[data-theme="dark"] .table.table.table tbody td span,
html[data-theme="dark"] .table.table.table tbody td span {
    color: #ffffff !important;
}

body[data-theme="dark"] .badge,
html[data-theme="dark"] .badge {
    background: rgba(0, 0, 0, 0.3) !important;
    color: #ffffff !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
}

body[data-theme="dark"] .badge.bg-success,
html[data-theme="dark"] .badge.bg-success {
    background: rgba(52, 199, 89, 0.3) !important;
    color: #34C759 !important;
}

body[data-theme="dark"] .badge.bg-secondary,
html[data-theme="dark"] .badge.bg-secondary {
    background: rgba(142, 142, 147, 0.3) !important;
    color: #8E8E93 !important;
}

body[data-theme="dark"] .badge i,
html[data-theme="dark"] .badge i {
    color: inherit !important;
}

/* 强制覆盖所有可能的样式 - 使用最强选择器 */
[data-theme="dark"] .table.table.table tbody td,
[data-theme="dark"] .table.table.table tbody td *,
[data-theme="dark"] .table.table.table thead th,
[data-theme="dark"] .card-body,
[data-theme="dark"] .card-header span {
    color: #ffffff !important;
}

[data-theme="dark"] .table.table.table {
    background: rgba(0, 0, 0, 0.2) !important;
}

[data-theme="dark"] .table.table.table thead {
    background: rgba(0, 0, 0, 0.3) !important;
}

[data-theme="dark"] .table.table.table tbody tr {
    background: rgba(0, 0, 0, 0.2) !important;
}

[data-theme="dark"] .card {
    background: rgba(0, 0, 0, 0.3) !important;
}

[data-theme="dark"] .card-header {
    background: rgba(0, 0, 0, 0.2) !important;
}

/* 额外的最强选择器确保覆盖Bootstrap */
body[data-theme="dark"] .table,
html[data-theme="dark"] .table,
body[data-theme="dark"] .table.table,
html[data-theme="dark"] .table.table {
    background: rgba(0, 0, 0, 0.2) !important;
}

body[data-theme="dark"] .table tbody td,
html[data-theme="dark"] .table tbody td,
body[data-theme="dark"] .table.table tbody td,
html[data-theme="dark"] .table.table tbody td {
    color: #ffffff !important;
    background: rgba(0, 0, 0, 0.2) !important;
}

body[data-theme="dark"] .table thead th,
html[data-theme="dark"] .table thead th,
body[data-theme="dark"] .table.table thead th,
html[data-theme="dark"] .table.table thead th {
    color: #ffffff !important;
    background: rgba(0, 0, 0, 0.3) !important;
}
</style>
{% endblock %}

{% block content %}
<!-- 页面标题 -->
<div class="page-title">
    <h2><i class="fas fa-users me-2"></i>用户管理</h2>
</div>

<!-- 用户管理卡片 -->
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span><i class="fas fa-list me-2"></i>用户列表</span>
        <a href="{{ url_for('add_user') }}" class="btn btn-primary btn-sm">
            <i class="fas fa-plus me-1"></i>添加用户
        </a>
    </div>
    <div class="card-body">
        <table class="table table">
            <thead>
                <tr>
                    <th><i class="fas fa-user me-1"></i>用户名</th>
                    <th><i class="fas fa-shield-alt me-1"></i>角色</th>
                    <th><i class="fas fa-key me-1"></i>权限</th>
                    <th><i class="fas fa-cogs me-1"></i>操作</th>
                </tr>
            </thead>
            <tbody>
                {% for user in users %}
                <tr>
                    <td>
                        <i class="fas fa-user-circle me-1"></i>{{ user.username }}
                    </td>
                    <td>
                        {% if user.is_admin %}
                            <span class="badge bg-success">
                                <i class="fas fa-crown me-1"></i>管理员
                            </span>
                        {% else %}
                            <span class="badge bg-secondary">
                                <i class="fas fa-user me-1"></i>普通用户
                            </span>
                        {% endif %}
                    </td>
                    <td>
                        <div class="d-flex flex-wrap gap-1">
                            {% if user.can_use_dingtalk %}
                                <span class="badge bg-info" title="钉钉消息任务">
                                    <i class="fas fa-robot me-1"></i>钉钉
                                </span>
                            {% endif %}
                            {% if user.can_use_custom_tasks %}
                                <span class="badge bg-warning" title="用户自定义任务">
                                    <i class="fas fa-tasks me-1"></i>自定义
                                </span>
                            {% endif %}
                            {% if user.can_manage_packages %}
                                <span class="badge bg-primary" title="依赖项管理">
                                    <i class="fas fa-boxes me-1"></i>依赖
                                </span>
                            {% endif %}
                            {% if user.can_manage_users %}
                                <span class="badge bg-danger" title="用户管理">
                                    <i class="fas fa-users me-1"></i>用户
                                </span>
                            {% endif %}
                        </div>
                    </td>
                    <td>
                        <div class="btn-group" role="group">
                            <a href="{{ url_for('edit_user', user_id=user.id) }}" 
                               class="btn btn-sm btn-warning {% if user.username == 'admin' %}disabled{% endif %}" 
                               {% if user.username == 'admin' %}tabindex="-1" aria-disabled="true" onclick="return false;"{% endif %}>
                                <i class="fas fa-edit me-1"></i>编辑
                            </a>
                            <a href="{{ url_for('delete_user_page', user_id=user.id) }}" 
                               class="btn btn-sm btn-danger {% if (user.is_admin and current_user.username != 'admin') or user.id == current_user.id %}disabled{% endif %}" 
                               {% if (user.is_admin and current_user.username != 'admin') or user.id == current_user.id %}tabindex="-1" aria-disabled="true" onclick="return false;"{% endif %}>
                                <i class="fas fa-trash me-1"></i>删除
                            </a>
                            <a href="{{ url_for('reset_password_page', user_id=user.id) }}" 
                               class="btn btn-sm btn-info {% if user.username == 'admin' and current_user.username != 'admin' %}disabled{% endif %}" 
                               {% if user.username == 'admin' and current_user.username != 'admin' %}tabindex="-1" aria-disabled="true" onclick="return false;"{% endif %}>
                                <i class="fas fa-key me-1"></i>重置密码
                            </a>
                            {% if user.id == current_user.id %}
                                <a href="{{ url_for('change_password') }}" class="btn btn-sm btn-success">
                                    <i class="fas fa-user-edit me-1"></i>修改密码
                                </a>
                            {% endif %}
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>

<script>
// 强制应用黑暗模式样式
document.addEventListener('DOMContentLoaded', function() {
    function applyDarkModeStyles() {
        if (document.body.getAttribute('data-theme') === 'dark') {
            // 强制应用黑暗模式样式
            const tableCells = document.querySelectorAll('.table tbody td, .table tbody td *');
            const tableHeaders = document.querySelectorAll('.table thead th, .table thead th *');
            const cardBody = document.querySelector('.card-body');
            const cardHeaderSpan = document.querySelector('.card-header span');
            const table = document.querySelector('.table');
            const tableRows = document.querySelectorAll('.table tbody tr');
            const tableHead = document.querySelector('.table thead');
            
            // 强制设置表格背景
            if (table) {
                table.style.background = 'rgba(0, 0, 0, 0.2)';
            }
            
            // 强制设置表格头部背景
            if (tableHead) {
                tableHead.style.background = 'rgba(0, 0, 0, 0.3)';
            }
            
            // 强制设置表格行背景
            tableRows.forEach(function(row) {
                row.style.background = 'rgba(0, 0, 0, 0.2)';
            });
            
            // 强制设置表格单元格文字颜色
            tableCells.forEach(function(element) {
                element.style.color = '#ffffff';
            });
            
            // 强制设置表格头部文字颜色
            tableHeaders.forEach(function(element) {
                element.style.color = '#ffffff';
            });
            
            if (cardBody) {
                cardBody.style.color = '#ffffff';
            }
            
            if (cardHeaderSpan) {
                cardHeaderSpan.style.color = '#ffffff';
            }
        }
    }
    
    // 初始应用
    applyDarkModeStyles();
    
    // 监听主题变化
    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {
                setTimeout(applyDarkModeStyles, 100);
            }
        });
    });
    
    observer.observe(document.body, {
        attributes: true,
        attributeFilter: ['data-theme']
    });
});
</script>
{% endblock %}